package quanlydoanra.utils;

import java.util.ArrayList;
import java.util.HashMap;

import dbutil.ConnectDB;
import dbutil.DBUtils;

public class managerID {

	/**************************************
	 * Method name		: FormatID
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static String FormatID(String code, int nextValue, int format){
		String result = "";
		int iNumber = nextValue;
		int k;
		String sFormat = "";
		for(k =0;k<format;k++){
			sFormat+="0";
		}
		result = code+sFormat+iNumber;
		return result;
	}

 /**************************************
 * Method name		: getNewID
 * Return type		: String
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static String getNewID(String tableName){		
		String sql = "select maquydinh,sokytuphiasauma,giatritieptheo,buocnhay from phatsinhidtudong where tenbang = '"+tableName+"'";
		//Buoc dau tien tao ra ID moi
		ArrayList<HashMap<String, Object>> arr = ConnectDB.getDataTypeList(sql);
		System.out.println("sizr "+arr.size());
		HashMap<String, Object> hash = (HashMap<String, Object>) arr.get(0);
		String maquydinh = hash.get("maquydinh").toString();
		String sokytuphiasau = hash.get("sokytuphiasauma").toString();
		String giatritieptheo = hash.get("giatritieptheo").toString();
		String buocnhay = hash.get("buocnhay").toString();
		String newID = FormatID(maquydinh, Integer.parseInt(giatritieptheo), Integer.parseInt(sokytuphiasau));
		//Buoi thu 2 update lai gia tri nextValue
		int nextStep =Integer.parseInt(giatritieptheo)+Integer.parseInt(buocnhay);
		System.out.println("Buoc nhay "+buocnhay+" nextStep "+nextStep+" New ID "+newID);
		String sql1 = "update phatsinhidtudong set giatritieptheo =  "+nextStep+" where tenbang = '"+tableName+"'";
		try{
			ConnectDB.connectDB();
			ConnectDB.getStatement().executeUpdate(sql1);
		}
		catch(Exception e){
			System.out.println("Loi");
		}
		return newID;
			
	}
 
}
